<?php

namespace Api\Controller;

use Think\Controller;
//use voku\helper\HtmlDomParser;

class IndexController extends Controller {

    public function _empty() {
        $this->index();
    }

    function index() {
        for($i=0; $i<=1000; $i++) {
            $url = 'https://gupiao.baidu.com/api/search/stockquery?from=pc&os_ver=1&cuid=xxx&vv=3.2&format=json&query_content='.$i.'&asset=0%2C4%2C14&timestamp=' . time();
            $res = file_get_contents($url);
            if ($res == false) {
                return;
            }
            $res = json_decode($res, true);
            if ($res['data']['stock_data']) {
                foreach ($res['data']['stock_data'] as $k => $v) {
                    $this->_add_gp_info($v);
                }
            }
        }
    }

    private function _add_gp_info($gp_data)
    {
        $GpCode = M('GpCode');
        if (!$GpCode->where(['f_code' => $gp_data['f_code']])->find()) {
            $gp_code_data = [
                'f_exchange'   => $gp_data['f_exchange'],
                'f_code'       => $gp_data['f_code'],
                'f_symbol'     => $gp_data['f_symbol'],
                'f_symbolName' => $gp_data['f_symbolName'],
                'f_fullName'   => $gp_data['f_fullName']
            ];
            $GpCode->add($gp_code_data);
        }
    }
}
